/**
 * 
 */
package systole.persistence.brokersDB;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import systole.domain.signals.SignalFrequency;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.SignalFrequencyBroker;
import systole.view.messages.ErrorMessages;

/**
 * @author jmj
 * 
 */
public class SignalFrequencyBrokerDB extends BrokerDB implements
        SignalFrequencyBroker {

    /**
     *
     */
    public SignalFrequencyBrokerDB() {
        super();
    }

    /*
     * (non-Javadoc)
     *
     * @seesystole.persistence.brokersInterface.SignalFrequencyBroker#
     * getAllSignalFrequency()
     */
    @Override
    public List<SignalFrequency> getAllSignalFrequency() throws ExceptionDAO {
        try {
            this.logger.logDebug("getting all frecuency");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            List<SignalFrequency> list = currentSession.createQuery("from SignalFrequency s order by upper(s.description)").list();
            this.logger.logDebug("getting frecuency successfully");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get all frecuency, msg: "
                    + e.getMessage());
            throw new ExceptionDAO(
                    "No se pudieron obtener los Periodos de Muestreo", e.fillInStackTrace());
        }
    }

    /*
     * (non-Javadoc)
     *
     * @seesystole.persistence.brokersInterface.SignalFrequencyBroker#
     * getSignalFrequencyById(long)
     */
    @Override
    public SignalFrequency getSignalFrequencyById(Integer id)
            throws ExceptionDAO {
        try {
            this.logger.logDebug("getting frequency ");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            SignalFrequency instance = (SignalFrequency) currentSession.get(SignalFrequency.class, id);
            this.logger.logDebug("get frequency successfully");
            return instance;
        } catch (HibernateException e) {
            this.logger.logError("error on get frequency, msg: "
                    + e.getMessage());
            throw new ExceptionDAO("No se pudo obtener el Período de Muestreo", e.fillInStackTrace());
        }
    }

    public void insert(SignalFrequency frequency) throws ExceptionDAO {
        try {
            this.logger.logDebug("save frequency ");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(frequency);
            this.logger.logDebug("save frequency successfully");
        } catch (HibernateException e) {
            this.logger.logError("error on save frequency, msg: "
                    + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    public void update(SignalFrequency frequency) throws ExceptionDAO {
        try {
            this.logger.logDebug("updating frequency ");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.update(frequency);
            this.logger.logDebug("update frequency");
        } catch (HibernateException e) {
            this.logger.logError("error on update frequency, msg: "
                    + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    @SuppressWarnings("unchecked")
    public boolean existSignalFrequency(String description, Integer id) throws ExceptionDAO {
        try {
            this.logger.logDebug("exist SignalFrequency");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(SignalFrequency.class).
                    add((id == null) ? Restrictions.eq("description", description)
                    : Restrictions.and(Restrictions.eq("description", description), Restrictions.ne("id", id)));
            List list = criteria.list();
            this.logger.logDebug("exist SignalFrequency successfully");
            return !list.isEmpty();
        } catch (HibernateException e) {
            this.logger.logError("error on exist SignalFrequency, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo verificar si ya existe el período de muestreo", e.fillInStackTrace());
        }
    }

    public SignalFrequency getSignalFrequencyByName(String description) throws ExceptionDAO {
        try {
            this.logger.logDebug("get SignalFrequency by description");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(SignalFrequency.class).
                    add(Restrictions.eq("description", description)).setMaxResults(1);
            return (SignalFrequency) criteria.uniqueResult();
        } catch (HibernateException e) {
            this.logger.logError("error on get SignalFrequency by description, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo obtener el período de muestreo", e.fillInStackTrace());
        }
    }
}
